//
//  MXNewViewShopsTableViewCell.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/7.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXNewViewShopsTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MXNewViewContentCollectionViewCell.h"
#import "MXNewViewDataModel.h"
#import "MXProductsModel.h"
#import "AFNetworkHandler.h"
#import "MXNewViewItemDetailVC.h"
#import "MXFindWebViewVC.h"
@interface MXNewViewShopsTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) MXProductsModel *model;

@property (nonatomic, retain) UIImageView *arrowheadImageView;
//设置解析数组
@property (nonatomic, retain) NSMutableArray *contentArray;
@end

@implementation MXNewViewShopsTableViewCell
- (void)dealloc
{
    [_arrowheadImageView release];
    [_navi release];
    [_NumArray release];
    [_contentArray release];
    [_productArray release];
    [_model release];
    [_tempArray release];
    [_control release];
    [_backImageView release];
    [_shopNameLabel release];
    [_shopTopView release];
    [_logoImageView release];
    [_detailLabel release];
    [_collectionView release];
    [_modelS release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self creatCollection];
        
        self.shopTopView = [[[UIView alloc]init] autorelease];
        [self.contentView addSubview:self.shopTopView];
        self.shopTopView.userInteractionEnabled = YES;
        // 添加点击手势
        UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(topViewAction:)];
        [self.shopTopView addGestureRecognizer:tap];
        [tap release];
        
        
        
        
        
        
        
        self.backImageView = [[[UIImageView alloc] init] autorelease];
        self.backImageView.backgroundColor = [UIColor whiteColor];
        [self.shopTopView addSubview:self.backImageView];

        self.logoImageView = [[[UIImageView alloc] init] autorelease];
        self.logoImageView.backgroundColor = [UIColor yellowColor];
        [self.backImageView addSubview:self.logoImageView];
        
        self.shopNameLabel = [[[UILabel alloc] init] autorelease];
        self.shopNameLabel.backgroundColor = [UIColor clearColor];
        [self.backImageView addSubview:self.shopNameLabel];
        
        
        self.detailLabel = [[[UILabel alloc] init] autorelease];
        self.detailLabel.backgroundColor = [UIColor whiteColor];
        self.detailLabel.font = [UIFont systemFontOfSize:16];
        [self.detailLabel setTextColor:[UIColor grayColor]];
        [self.shopTopView addSubview:self.detailLabel];
        
        self.tempArray = [NSMutableArray array];
        self.productArray = [NSMutableArray array];
        self.contentArray = [NSMutableArray array];
        self.NumArray = [NSMutableArray array];
        
        self.arrowheadImageView = [[[UIImageView alloc] init] autorelease];
        self.arrowheadImageView.image = [UIImage imageNamed:@"right"];
        self.arrowheadImageView.userInteractionEnabled = YES;
        [self.backImageView addSubview:self.arrowheadImageView];
        

    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
// 自定义Cell的总表头
    CGFloat WID = self.contentView.frame.size.width;
    CGFloat margin = 15;
    CGFloat TopViewY = 0;
    CGFloat TopViewH = HEIGHTPRO * 40;
    self.shopTopView.frame = CGRectMake(margin, TopViewY, WID - 2 * margin, TopViewH);
// 店家的logo
    CGFloat logoX = 0;
    CGFloat logoY = 0;
    CGFloat logoW = TopViewH;
    CGFloat logoH = TopViewH;
    self.logoImageView.frame = CGRectMake(logoX, logoY, logoW, logoH);
// 店家名称
    CGFloat shopNameX = 0;
    CGFloat shopNameY = 0;
    CGFloat shopNameW = WID - margin;
    CGFloat shopNameH = TopViewH;
//设置背景图片
    self.backImageView.frame = CGRectMake(shopNameX, shopNameY, shopNameW - margin, shopNameH);
    self.backImageView.image = [UIImage imageNamed:@"shopBackground"];
    self.backImageView.layer.cornerRadius = WIDTHPRO * 10;
    self.backImageView.layer.masksToBounds = YES;
    
    self.shopNameLabel.frame = CGRectMake(TopViewH, shopNameY, shopNameW, shopNameH);
    
//设置小箭头
    self.arrowheadImageView.frame = CGRectMake(self.backImageView.frame.size.width - WIDTHPRO * 40, HEIGHTPRO * 5, WIDTHPRO * 30, HEIGHTPRO * 30);
    
#warning 测试Target-Action方法
    
// 商店标语
    CGFloat detailX = margin;
    CGFloat detailY = TopViewY + TopViewH + HEIGHTPRO * 5;
    CGFloat detailW = WID - WIDTHPRO * 30;
    CGFloat detailH = HEIGHTPRO * 20;
    self.detailLabel.frame = CGRectMake(detailX, detailY, detailW, detailH);
// 设置CollectionView 坐标
    CGFloat collectionX = WIDTHPRO * 20;
    CGFloat collectionY = detailY + detailH + HEIGHTPRO * 10;
    CGFloat collectionW = WID - 2 * collectionX;
    CGFloat collectionH = HEIGHTPRO * 220;
    self.collectionView.frame = CGRectMake(collectionX, collectionY, collectionW, collectionH);
}
- (void)creatCollection
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 105, HEIGHTPRO * 105);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(1 , 1 , 1, 1);
#pragma mark - 根据TopView手动更改
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    
    CGFloat CELL_width = self.frame.size.width;
    CGFloat CELL_height = self.frame.size.height;
    CGFloat SUB = self.shopTopView.frame.origin.y + self.shopTopView.frame.size.height + self.detailLabel.frame.size.height + HEIGHTPRO * 5;
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHTPRO * 65, CELL_width, CELL_height - SUB) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXNewViewContentCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}
#pragma mark - CollectionView 协议实现部分
/************************* CollectionView Delegate **************************/
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
#warning productArray
    return _tempArray.count;
}
//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXNewViewContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor magentaColor];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
//#warning 8/8/16:54 未解决
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
//#warning Collection赋值的时候有问题!!!
#warning 8/11 10:00 已解决


    
    [cell GetInfo:self.tempArray[indexPath.row]];
    

    return cell;
    
}
#pragma mark - collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXNewViewItemDetailVC *detailVC = [[[MXNewViewItemDetailVC alloc] init] autorelease];
    
    detailVC.itemID = [self.tempArray[indexPath.row] itemid];
    
    detailVC.priceString = [self.tempArray[indexPath.row] price];

    detailVC.naviTemp = self.navi;
    
    [self.navi pushViewController:detailVC animated:YES];
}
/*
//设置HeaderView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *topView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    return topView;
}
*/
#pragma mark - 赋值方法
-(void)GetInfo:(MXNewViewDataModel *)model
{
    if (_modelS != model) {
        [_modelS release];
        _modelS = [model retain];
    }
    
    self.shopNameLabel.text = model.shopName;
    
    self.detailLabel.text = model.shopSlogon;
}

-(void)GetDetailInfo:(MXNewViewDataModel *)infoModel
{
    if (_modelS != infoModel) {
        [_modelS release];
        _modelS = [infoModel retain];
    }
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:infoModel.picUrl] placeholderImage:[UIImage imageNamed:@"logo_pleaseholder"]];
   
}

// 手势方法
- (void)topViewAction:(UITapGestureRecognizer *)tap
{
    MXFindWebViewVC *find = [[MXFindWebViewVC alloc]init];
    find.requestString = self.modelS.shopUrl;
    find.TitleString = self.modelS.shopName;
    [self.viewContrller.navigationController pushViewController:find animated:YES];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
